package com.jungle.mvvm.sample.ui.mine;

import android.annotation.SuppressLint;
import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.view.View;

import com.jungle.mvvm.base.BaseViewModel;
import com.jungle.mvvm.binding.command.BindingCommand;
import com.jungle.mvvm.bus.event.SingleLiveEvent;
import com.jungle.mvvm.sample.annotation.Authorize;
import com.jungle.mvvm.sample.app.WanObserver;
import com.jungle.mvvm.sample.bean.SameBean;
import com.jungle.mvvm.sample.bean.UserBean;
import com.jungle.mvvm.sample.http.WanNetwork;
import com.jungle.mvvm.sample.ui.collect.CollectFragment;
import com.jungle.mvvm.sample.ui.login.LoginModel;
import com.jungle.mvvm.sample.ui.toolbar.ToolBarViewModel;
import com.jungle.mvvm.sample.util.LoginUtil;
import com.jungle.mvvm.utils.ToastUtils;

import skin.support.SkinCompatManager;

/**
 * @ClassName MineViewModel
 * @Description 我的
 * @Author Daniel
 * @Version V1.0.0
 */
public class MineViewModel extends BaseViewModel {
    public String mUrl = "https://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png";

    public ObservableField<Boolean> hasLogined = new ObservableField<>(false);

    public ToolBarViewModel toolBarViewModel;


    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> updateEvent = new SingleLiveEvent<>();
    }

    public MineViewModel(@NonNull Application application) {
        super(application);
        toolBarViewModel = new ToolBarViewModel(this);
        toolBarViewModel.leftVisible.set(View.GONE);
        toolBarViewModel.title.set("我的");
        hasLogined.set(LoginUtil.hasLogined());
    }

    public BindingCommand collectOnClickCommand = new BindingCommand(this::toCollect);

    public BindingCommand coinOnClickCommand = new BindingCommand(this::toCoin);

    public BindingCommand updateOnClickCommand = new BindingCommand(this::update);

    public BindingCommand logoutOnClickCommand = new BindingCommand(this::logout);

    @Authorize
    private void toCollect() {
        // SkinCompatManager.getInstance().loadSkin("night", SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); // 后缀加载
        startContainerActivity(CollectFragment.class.getCanonicalName());
    }

    private void toCoin() {
        SkinCompatManager.getInstance().restoreDefaultTheme();
        // startContainerActivity(CoinFragment.class.getCanonicalName());
    }

    private void update() {
        uc.updateEvent.setValue(uc.updateEvent.getValue());
    }

    @SuppressLint("CheckResult")
    @Authorize
    private void logout() {
        LoginModel.logout()
                .compose(WanNetwork.applyService(getLifecycleProvider(), new WanObserver<SameBean<UserBean>>() {
                    @Override
                    public void onWanSuccess(SameBean<UserBean> userBeanSameBean) {
                        LoginUtil.clearLogin();
                        hasLogined.set(false);
                    }

                    @Override
                    public void onWanFailure(String e) {
                        ToastUtils.showShort("退出登录失败：" + e);
                    }
                }));
    }

}
